﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmLoaiNguyenLieu : Form
    {
        
        private LoaiNguyenLieuService httService = new LoaiNguyenLieuService();

        public void LoadThongTin(DataTable dbtab, DataTable dbtab1)
        {

            lv_LoaiNL.Items.Clear();
            lv_DVTinh.Items.Clear();
            //DataTable dbtab = nvService.LayTatCaNV();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                lv_LoaiNL.Items.Add(liv);
            }
            for (int i = 0; i < dbtab1.Rows.Count; i++)
            {
                DataRow dbrow = dbtab1.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                lv_DVTinh.Items.Add(liv);
            }
        }

        public frmLoaiNguyenLieu()
        {
            InitializeComponent();
        }

        private void frmLoaiNguyenLieu_Load(object sender, EventArgs e)
        {
            DataTable dbtab = httService.LoadLoaiNguyenLieu();
            DataTable dbtab1 = httService.LoadDonViTinh();
            LoadThongTin(dbtab, dbtab1);
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            string LoaiNL = txt_LoaiNL.Text;
            if (!httService.ThemLoaiNguyenLieu(LoaiNL))
            {
                MessageBox.Show("Thêm Không Thành Công");
                return;
            }
            DataTable dbtab = httService.LoadLoaiNguyenLieu();
            DataTable dbtab1 = httService.LoadDonViTinh();
            LoadThongTin(dbtab, dbtab1);
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            if (lv_LoaiNL.FocusedItem.Index > -1)
            {
                string LoaiNL = txt_LoaiNL.Text;
                if (LoaiNL != "")
                {
                    int index = lv_LoaiNL.FocusedItem.Index;
                    int ma = int.Parse(lv_LoaiNL.Items[index].Text.ToString());
                    if (!httService.SuaLoaiNguyenLieu(ma, LoaiNL))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        return;
                    }
                    txt_LoaiNL.Text = "";
                    DataTable dbtab = httService.LoadLoaiNguyenLieu();
                    DataTable dbtab1 = httService.LoadDonViTinh();
                    LoadThongTin(dbtab, dbtab1);
                }else
                {
                    MessageBox.Show("Không Thể để trống");
                    return;
                }
            }else
                MessageBox.Show("Chưa chọn loại nguyên liệu");
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            if (lv_LoaiNL.FocusedItem.Index > -1)
            {
                int index = lv_LoaiNL.FocusedItem.Index;
                int ma = int.Parse(lv_LoaiNL.Items[index].Text.ToString());
                if (!httService.XoaLoaiNguyenLieu(ma))
                {
                    MessageBox.Show("Xóa Không Thành Công");
                    return;
                }
                txt_LoaiNL.Text = "";
                DataTable dbtab = httService.LoadLoaiNguyenLieu();
                DataTable dbtab1 = httService.LoadDonViTinh();
                LoadThongTin(dbtab, dbtab1);

            }
        }

        private void btn_huy_Click(object sender, EventArgs e)
        {

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            string DVTinh = txt_DVTinh.Text;
            if (!httService.ThemDonViTinh(DVTinh))
            {
                MessageBox.Show("Thêm Không Thành Công");
                return;
            }
            DataTable dbtab = httService.LoadLoaiNguyenLieu();
            DataTable dbtab1 = httService.LoadDonViTinh();
            LoadThongTin(dbtab, dbtab1);
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            if (lv_DVTinh.FocusedItem.Index > -1)
            {
                string ten = txt_DVTinh.Text;
                if (ten != "")
                {
                    int index = lv_DVTinh.FocusedItem.Index;
                    int ma = int.Parse(lv_DVTinh.Items[index].Text.ToString());
                    if (!httService.SuaDonViTinh(ma, ten))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        return;
                    }
                    txt_DVTinh.Text = "";
                    DataTable dbtab = httService.LoadLoaiNguyenLieu();
                    DataTable dbtab1 = httService.LoadDonViTinh();
                    LoadThongTin(dbtab, dbtab1);
                }else
                {
                    MessageBox.Show("Không Thể để trống");
                    return;
                }
            }
            else
                MessageBox.Show("Chưa chọn loại nguyên liệu");
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (lv_DVTinh.FocusedItem.Index > -1)
            {
                int index = lv_DVTinh.FocusedItem.Index;
                int ma = int.Parse(lv_DVTinh.Items[index].Text.ToString());
                if (!httService.XoaDonViTinh(ma))
                {
                    MessageBox.Show("Xóa Không Thành Công");
                    return;
                }
                txt_DVTinh.Text = "";
                DataTable dbtab = httService.LoadLoaiNguyenLieu();
                DataTable dbtab1 = httService.LoadDonViTinh();
                LoadThongTin(dbtab, dbtab1);
            }
            else
            {
                MessageBox.Show("Chưa chọn dữ liệu xóa");
                return;
            }

        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {

        }

        private void lv_LoaiNL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_LoaiNL.FocusedItem.Index > -1)
            {
                int index = lv_LoaiNL.FocusedItem.Index;
                int ma = int.Parse(lv_LoaiNL.Items[index].Text.ToString());
                txt_LoaiNL.Text = httService.LayLoaiNguyenLieuLieuTheoMa(ma);
            }
        }

        private void lv_DVTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lv_DVTinh.FocusedItem.Index > -1)
            {
                int index = lv_DVTinh.FocusedItem.Index;
                int ma = int.Parse(lv_DVTinh.Items[index].Text.ToString());
                txt_DVTinh.Text = httService.LayDonViTinhTheoMa(ma);
            }
        }

    }
}
